package com.cn.kehong.service;


import com.cn.kehong.api.IPhysicalChemicalWaterService;
import com.cn.kehong.dao.*;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.*;
import com.google.common.collect.Lists;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PhysicalChemicalWaterServiceImpl implements IPhysicalChemicalWaterService {

    @Autowired
    private PhysicaChemicalWaterMapper physicaChemicalWaterMapper;
    @Autowired
    private PhysicalChemicalMapper physicalChemicalMapper;


    @Override
    public String addBatchWater(List<PhysicaChemicalWater> physicaChemicalWaters) throws Exception {
        List<PhysicaChemicalWater> waterNos = Lists.newArrayList();
        for (PhysicaChemicalWater physicaChemicalWater:physicaChemicalWaters){
            String result = this.waterLegal(physicaChemicalWater);
            if (!"".equals(result)){
                return result;
            }
            String water = CommonUtil.generateRandomNum("water_");
            physicaChemicalWater.setId(water);
            physicaChemicalWater.setCreateTime(new Date());
            waterNos.add(physicaChemicalWater);
        }
        int count = physicaChemicalWaterMapper.batchInsertWaters(waterNos);
        if (count>0){
            for (PhysicaChemicalWater physicaChemicalWater :physicaChemicalWaters){
                PhysicalChemicalExample chemicalExample = new PhysicalChemicalExample();
                 chemicalExample.createCriteria().andIdEqualTo(physicaChemicalWater.getPhysicalId());
                PhysicalChemical chemical = new PhysicalChemical();
                chemical.setStatus("1");
                chemical.setUpdateTime(new Date());
                chemical.setSubmissionTime(new Date());
                chemical.setUpdateBy(physicaChemicalWater.getCreateBy());
                physicalChemicalMapper.updateByExampleSelective(chemical,chemicalExample);
            }
        }
        return CommonUtil.outStr(count);
    }

    private String waterLegal(PhysicaChemicalWater physicaChemicalWater) {

        String result = "";

        if (StringUtils.isBlank(physicaChemicalWater.getPpm())){
            result = "请输入检验结果(ppm)";
            return result;
        }
        return result;
    }
}
